package cn.edu.xmu.software.ijoker.UI;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;
import cn.edu.xmu.software.ijoker.R;
import cn.edu.xmu.software.ijoker.entity.Joke;

public class JokeList extends Activity {
	private ListView itemlist;
	private List<Joke> jokeList;
	// private IPlayService playService;
	private int player_position = 0;

	// private String jokes[] = { "小白与毛毛的故事", "我的小狗叫藏獒", "一切尽在哥的掌握中", "日插操日他" };

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.jokelist);
		// this.bindService(new Intent(JokeList.this, PlayService.class),
		// serviceConnection, Context.BIND_AUTO_CREATE);
		itemlist = (ListView) findViewById(R.id.JokeList);
		/*
		 * jokeAdapter=new ArrayAdapter<String>(this,
		 * android.R.layout.simple_list_item_1, jokes);
		 * jokeList.setAdapter(jokeAdapter);
		 */

		// 生成动态数组，加入数据
		jokeList = new ArrayList<Joke>();
		Joke joke1 = new Joke();
		joke1.setAuthor("邱鸿斌");
		joke1.setId(1);
		joke1.setLocation("http://59.77.5.42:80/jokes/real.mp3");
		joke1.setTitle("joke");
		joke1.setUploadTime(new Date());
		jokeList.add(joke1);
		Joke joke2 = new Joke();
		joke2.setAuthor("白志斌");
		joke2.setId(1);
		joke2.setLocation("http://59.77.5.42:80/jokes/real.mp3");
		joke2.setTitle("joke");
		joke2.setUploadTime(new Date());
		jokeList.add(joke2);
		Joke joke3 = new Joke();
		joke3.setAuthor("邱鸿斌");
		joke3.setId(1);
		joke3.setLocation("http://59.77.5.42:80/jokes/real.mp3");
		joke3.setTitle("joke");
		joke3.setUploadTime(new Date());
		jokeList.add(joke3);
		updateJokeList();
	}

	// private ServiceConnection serviceConnection = new ServiceConnection() {
	// public void onServiceConnected(ComponentName className, IBinder service)
	// {
	// playService = IPlayService.Stub.asInterface((IBinder) service);
	// if (jokeList != null)
	// {
	// Iterator<Joke> iterator = jokeList.iterator();
	// while (iterator.hasNext()) {
	// Joke joke = iterator.next();
	// try {
	// playService.addJokeList(joke.getLocation());
	// } catch (RemoteException e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	// }
	// }
	// }
	//
	// public void onServiceDisconnected(ComponentName className) {
	// playService = null;
	// }
	// };

	// 更新笑话列表
	public void updateJokeList() {
		List<Map<String, Object>> list = buildPlayListForSimpleAdapter();
		// 生成适配器的Item和动态数组对应的元素
		SimpleAdapter listItemAdapter = new SimpleAdapter(this, list,// 数据源
				R.layout.jokelist_style,// ListItem的XML实现
				// 动态数组与ImageItem对应的子项
				new String[] { "ItemImage", "ItemTitle", "ItemText" },
				// ImageItem的XML文件里面的一个ImageView,两个TextView ID
				new int[] { R.id.ItemImage, R.id.ItemTitle, R.id.ItemText });

		// 添加并且显示
		itemlist.setAdapter(listItemAdapter);

		itemlist.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				setTitle("点击第" + arg2 + "个项目");
				player_position = arg2;

				// stream the joke;
				// try {
				// playService.play(arg2);
				// } catch (RemoteException e) {
				// // TODO Auto-generated catch block
				// e.printStackTrace();
				// }
				Intent intent = new Intent();
				intent.setClass(JokeList.this, PlayerUI.class);
				Joke joke = jokeList.get(arg2);
				Bundle jokeInfo = new Bundle();
				jokeInfo.putSerializable("joke", joke);
				intent.putExtra("android.intent.extra.jokeInfo", jokeInfo);
				startActivityForResult(intent, 0);
			}

		});

		// 添加长按点击
		itemlist
				.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {

					@Override
					public void onCreateContextMenu(ContextMenu menu, View v,
							ContextMenuInfo menuInfo) {
						menu.setHeaderTitle("长按菜单-ContextMenu");
						menu.add(0, 0, 0, "弹出长按菜单0");
						menu.add(0, 1, 0, "弹出长按菜单1");
					}
				});
	}

	private List<Map<String, Object>> buildPlayListForSimpleAdapter() {
		// try {
		// playService.clearJokeList();
		// } catch (RemoteException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		if (jokeList != null) {
			ArrayList<Map<String, Object>> list = new ArrayList<Map<String, Object>>(
					jokeList.size());
			Iterator<Joke> iterator = jokeList.iterator();
			while (iterator.hasNext()) {
				Joke joke = (Joke) iterator.next();
				// try {
				// playService.addJokeList(joke.getLocation());
				// } catch (RemoteException e) {
				// // TODO Auto-generated catch block
				// e.printStackTrace();
				// }
				HashMap<String, Object> map = new HashMap<String, Object>();
				map
						.put("ItemImage",
								android.R.drawable.checkbox_off_background);// 图像资源的ID
				map.put("ItemTitle", joke.getTitle());
				map.put("ItemText", joke.getAuthor());
				list.add(map);
			}
			return list;
		}
		return null;

	}

	// 长按菜单响应函数
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		setTitle("点击了长按菜单里面的第" + item.getItemId() + "个项目");
		return super.onContextItemSelected(item);
	}

}
